﻿using System.Web.Mvc;

namespace CodeContrib.Web.Mvc.Html
{
	public static class TwitterExtensions
	{
		public static MvcHtmlString TwitterButton(this HtmlHelper html, string innerHtml, TwitterButtonDataCount dataCount, string username)
		{
			return TwitterButton(html, innerHtml, dataCount, username, null, null);
		}

		public static MvcHtmlString TwitterButton(this HtmlHelper html, string innerHtml, TwitterButtonDataCount dataCount, string username, string url, string title)
		{
			var tag = new TagBuilder("a")
				          {
					          InnerHtml = innerHtml
				          };
			tag.AddCssClass("twitter-share-button");
			tag.Attributes["data-count"] = dataCount.ToString();
			tag.Attributes["href"] = "http://twitter.com/share";

			if (!string.IsNullOrWhiteSpace(title))
			{
				tag.Attributes["data-text"] = title;
			}

			if (!string.IsNullOrWhiteSpace(url))
			{
				tag.Attributes["data-url"] = url;
				tag.Attributes["data-counturl"] = url;
			}

			if (!string.IsNullOrWhiteSpace(username))
			{
				tag.Attributes["data-via"] = username;
			}

			return MvcHtmlString.Create(tag.ToString());
		}
	}
}